
var db = null;
var request,objStore1;
var DBName = "myBooks",DBVersion = 1;
var bookLists = [{		title: "Web前端开发技术-Html、Css、JavaScript",
		author: "储久良",		isbn: "9787302431695"	},
	{	title: "计算机组成原理(修订版)",		author: "张功萱",
		isbn: "9787302433637"	},
	{	title: "HTML/CSS/JavaScript标准教程",
		author: "本书编委会",		isbn: "9787121079344"	},
	{	title: "HTML+CSS网页设计与布局从入门到精通",
		author: "温谦",		isbn: "9787115183392"	},
	{	title: "Java 2实用教程(第5版)",
		author: "耿祥义",		isbn: "9787302464259 "	}];

var indexedDB = window.indexedDB || window.mozIndexedDB || window.msIndexedDB || window.webkitIndexedDB;

function createDB(dbName, dbVersion) {
	request = indexedDB.open(dbName, dbVersion);
	request.onerror = function(event) {
		alert("打开数据库失败:" + event.target.errorCode);
		console.log("打开数据库失败:" + event.target.errorCode);
	}
	request.onsuccess = function(event) {
		alert("打开数据库成功!");
		//console.log("打开数据库成功!");
		db = event.target.result;
		var trans1 = db.transaction(["books"], "readwrite");
		objStore1 = trans1.objectStore("books");
	}
	request.onupgradeneeded = function(event) {
		alert("版本变化！" + "版本号为" + event.newVersion);
		console.log("版本变化！" + event.newVersion);
		db = event.target.result;
		//为books对象仓库创建事件对象
		//var trans1 = db.transaction(["books"],"readwrite");
		if(!db.objectStoreNames.contains("books")){
		objStore1 = db.createObjectStore("books", {keyPath: "isbn"});
		objStore1.createIndex("by_title", 'title', {unique: false});
		objStore1.createIndex("by_author", 'author', {unique: false});
		objStore1.createIndex("by_isbn", 'isbn', {unique: true});
		}
		loadBooks(); //初始化图书
		window.location.reload();
		window.location.hash = "#list";
		$("booklist").value = "";
	}
}

createDB(DBName, DBVersion);
function $(id) {
	return document.getElementById(id);
}

function loadBooks() {
	$("booklist").innerHTML = ""; //加载前先清空列表
	alert("开始装载图书....");
	var trans=db.transaction("books","readwrite");
	var objStore1=trans.objectStore("books");
	for(var i = 0; i < bookLists.length; i++) {
		var request = objStore1.put(bookLists[i]);
		request.onerror = function() {
			console.error('数据库中已有该对象,不能重复添加！！');
		};
		request.onsuccess = function() {
			console.log('对象已成功存入对象仓库中！')
			//console.dir(request.value);
		};
	}
}
function showBooks() {
	var query = document.forms.list.query.value;
	$("booklist").value = "";
	var transaction = db.transaction(["books"], "readonly");
	var objStore = transaction.objectStore("books");
	var index = objStore.index("by_title");
	var range = IDBKeyRange.bound(query,query+"z");
	//alert(range.value);
	var result = (query.length > 0) ? index.openCursor(range) : index.openCursor();
	var i = 1;
	result.onsuccess = function(e) {
		var cursor = e.target.result;
		if(cursor) {
			text1 = i + "-" + cursor.value.title + "," + cursor.value.author + "," + cursor.value.isbn;
			$("booklist").value +=text1 + "\n"
			cursor.continue();
			i++;
		} else {
			console.log("没有检索内容");
			//$("booklist").value = "没有检索到所需要的图书！"
		}
	};
	result.onerror = function(e) {console.log("检索失败！")};
}
function addBook() {
	var title = document.add.title.value;
	var author = document.add.author.value;
	var isbn = document.add.isbn.value;
	var onebook = {
		title: title,
		author: author,
		isbn: isbn
	};
	if(title.length > 0 && author.length > 0 && isbn.length > 0) {
		var trans2 = db.transaction("books", "readwrite");
		var objStore2 = trans2.objectStore("books"); //获取books对象仓库
		var request1 = objStore2.add(onebook);
		trans2.oncomplete = function(event) {
			alert("图书成功添加！界面即将被清空！");
			document.forms.add.title.value = "";
			document.forms.add.author.value = "";
			document.forms.add.isbn.value = "";
			window.location.reload();
			//location.hash="#list";
		};
	}
}
function deleteDatabase() {
	if(indexedDB) {
		var deleteDB = indexedDB.deleteDatabase("books");
		deleteDB.onsuccess = function(e) {
			alert("数据库删除成功，即将重新初始化...");
			window.location.reload();
		}
	}
}
function deleteAllBooks() {
	var trans1 = db.transaction("books", "readwrite");
	var objStore1 = trans1.objectStore("books");
	objStore1.clear();
	trans1.oncomplete = function(e) {
		alert("所有图书清除成功！");
	}
	trans1.onerror = function(e) {
		alert("所有图书清除失败！");
	}
	window.location.reload();
}
/* 监听hashchange事件，并绑定事件处理函数
       根据hash值，动态地给body修改class属性的值
       完成Section随导航自动切换
 */
window.addEventListener('hashchange', function() {
	switch(location.hash) {
		case "#add":
			document.body.className = "add";
			break;
		case "#setting":
			document.body.className = "setting";
			break;
		default:
			document.body.className = "list";
	}
}, false);
